import api from '../../api';
import deviceInfo from '@ohos.deviceInfo';
import uuid from '../../../others/uuid';
import systemDateTime from '@ohos.systemDateTime';
import http from '@ohos.net.http';
import kvStoreUtil from '../../../kvStore/kvStoreUtil'
import log from '../../../logutil';
export default class getFp extends api{
  public static async getInstance(){
    let seed_id = randstr(16)
    let seed_time = (await systemDateTime.getCurrentTime(false)).toString()
    let deviceId;
    try{
      deviceId = (await kvStoreUtil.getInstance().get("MHY-DeviceId")).trim();
    }catch (e){
      deviceId = uuid()
      kvStoreUtil.getInstance().put("MHY-DeviceId",deviceId)
    }
    let devicefp;
    try{
      devicefp = kvStoreUtil.getInstance().get("MHY-DeviceFp");
    }catch (e){
      log.warn(getFp.name,JSON.stringify(e))
    }

    return new getFp(deviceId,seed_id,seed_time,devicefp)
  }
  private constructor(deviceId,seed_id,seed_time,devicefp?) {
    super({
      url:"https://public-data-api.mihoyo.com/device-fp/api/getFp",
      body:{
        device_id:deviceId,
        seed_id,
        seed_time,
        platform:"5",
        device_fp:getFp.getFakeDeviceFp(),
        app_name:"account_cn",
        ext_fields:JSON.stringify(new ext_fields())
      },
      method:http.RequestMethod.POST
    })
  }

  private static getFakeDeviceFp(){
    const Random = ()=>{
      return Math.floor(Math.random()*10)+''
    }
    let i = 10
    let str = Random()
    let i2 = 0;
    let i3 = i-2;
    if(i3>=0){
      while(true){
        str+=Random()
        if(i2==i3){
          break;
        }
        i2++;
      }
    }
    return str
  }
}
function randstr(num){
  const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
  let str = ''
  const Random = ()=>{
    return Math.floor(chars.length*10)+''
  }
  for(let i=0;i<num;i++){
    str += Random()
  }
  return str;
}
class ext_fields{
  browserLanguage="zh-CN"
  browserPlat="Linux i686"
  browserScreenSize="265440"
  browserTimeZone="Asia/Beijing"
  cpuClass="unknown"
  deviceMemory="4"
  hardwareConcurrency="16"
  hasLiedBrowser=0
  hasLiedOs=0
  hasLiedResolution=1
  ifAdBlock=0
  ifNotTrack="unkown"
  isTouchSupported=true
  listOfPlugins="unknown"
  maxTouchPoints=5
  numOfPlugins=0
  screenRatio=1
  userAgent=`Mozilla/5.0 (Linux; Android 13; Pixel 5;) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/113.0.5672.131 Mobile Safari/537.36 miHoYoBBS/2.40.1`
  webGlRender="Subsystem OpenGL ES Translator"
  webGlVendor="Microsoft"

  constructor() {

  }
}